package QueryHandler;

import java.util.Vector;

/*
 * Datalog Query class
 */
public class DLQuery {

	private Vector<RelationVar> headVars;
	private Vector<Relation> relations;

/*
 * Constructor for the first time
 */
	
	public DLQuery(Vector<RelationVar> headInput, Vector<Relation> relationsInput){
		headVars = headInput;
		relations = relationsInput;
	}

/*
 * Constructor for building the Query while computing
 * the safe plan algorithm
 */
	
	public DLQuery(DLQuery oldQuery, RelationVar addToHead){			
		headVars = oldQuery.headVars;
		relations = oldQuery.relations;
		headVars.add(addToHead);		
	}

/*
 * Gets all the fields (not Predicate) in the relation
 * that a not in the head - > meaning its a Free var
 */
	
	public Vector<RelationVar> getFreeVar() {
		Vector<RelationVar> ans = new Vector<RelationVar>();
		for (Relation r : relations){
			for (RelationVar ro : r.getVars()){
				if(!strContains(ro)){
					ans.add(ro);
				}
			}
		}
		return ans;
	}

/*
 * checks r is a Free var and not in the head
 */
	
	public boolean strContains(RelationVar r){
		for (int i = 0 ; i < headVars.size() ; i++){
			if (headVars.elementAt(i).getName().equals(r.getName()))
					return true;
		}
		return false;		
	}
	
//*********************************//
/*  	Getters & Setters		   */
//*********************************//
	
	public Vector<RelationVar> getHeadVars() {
		return headVars;
	}

	public void setHeadVars(Vector<RelationVar> headVars) {
		this.headVars = headVars;
	}

	public Vector<Relation> getRelations() {
		return relations;
	}

	public void setRelations(Vector<Relation> relations) {
		this.relations = relations;
	}

}
